<?php

Class PlusModel extends Model{

    /**
     * 获得单条数据
     * @param $condition 条件
     * @return mixed
     */
    public function getPlusByOne($condition,$field){
        return $this->where($condition)->field($field)->find();
    }

    /**
     * 获得多条数据
     * @param $condition 条件
     * @param $field 字段
     * @param $order 排序
     * @param $limit 数量
     * @return mixed
     */
    public function getPlusByMore($condition,$field,$order,$limit){
        return $this->where($condition)->field($field)->order($order)->limit($limit)->select();
    }

    /**
     * @param $data 数据
     * @return mixed|void
     */
    public function insert($data){
        if(empty($data)){
            return;
        }
        return $this->add($data);
    }

    /**
     * 更新数据
     */
    public function updata($condition,$data){
        return $this->where($condition)->save($data);
    }

    /**
     * 添加插件数据
     */
    public function addPlus($data){

        $insertdata['title'] = $data['title'];
        $insertdata['name'] = $data['name'];
        $insertdata['type'] = $data['type'];
        $insertdata['keyword'] = $data['keywords'];
        $insertdata['description'] = $data['description'];
        $insertdata['uid'] = cookie('loginId');
        $insertdata['created_at'] = time();
        $file = $_FILES['file_pic'];
        $zip = $_FILES['zip'];

        /**
         * 上传压缩文件
         */
        if($_FILES['zip']['error'] != 4){
            $plusPath = "Data/Upload/plus/zip/";
            $uploadres = $this->_uploadfile($zip,$plusPath);
            if($uploadres['code']==200){
                $insertdata['zip'] = $uploadres['savepath'] ;
            }
        }

        /**
         * 上传缩略图
         */
         if($_FILES['file_pic']['error']!=4){
             $thumbPath = "Data/Upload/plus/thumb/";
             $uploadres = $this->_uploadfile($file,$thumbPath);
             if($uploadres['code']==200) {
                 $insertdata['thumb'] = $uploadres['savepath'];
             }
         }
         if($data['id']) {
               $where['id'] = $data['id'];
               $res = $this->where($where)->save($insertdata);
         }else{
               $res =  $this->insert($insertdata);
         }
         if($res){
               $res = ['status'=>200,'msg'=>'success'];
               return $res;
         }
         $res = ['status'=>400,'msg'=>'插件保存失败'];
         return $res;
    }

    public function getPlusListByPage($request){
         $data = [];
         $page = $request['page']?$request['page']:1;
         $where['uid']  = cookie('loginId');
         $total = $this->where($where)->count();
         $pageSize = 10;
         $startLimit = ($page-1)*$pageSize;
         $data = $this->getPlusByMore($where,'','',"$startLimit,$pageSize");
         $data = $this->connectPlusType($data);
         // 当数量大于当前页数
         if($total>$pageSize){
             $url = "__URL__/plus?";
             $pagestr = $this->page($total,$page,$pageSize,$url);
             $data['pagestr'] = $pagestr;
         }
         return $data;
    }

    /**
     * 关联类别
     * @param $data
     */
    public function connectPlusType($data){
         if(!is_array($data)){
               return;
         }
         foreach($data as $i => $v){
              $where['id'] = $v['type'];
              $typename = M("plus_type")->where($where)->getField('name');
              $data[$i]['typename'] = $typename;
         }
         return $data;
    }

    /**
     * 插件id
     * @param $id
     */
    public function delPlus($id){
         $where['id'] = $id;
         $plus = $this->getPlusByOne($where);
         if(!$plus){
               // 该插件不存在
               $res = ['status'=>400,'msg'=>"该插件不存在"];
               return $res;
         }
         $res = $this->where(array('id'=>$id))->delete();
         if($res){
              // 删除文件
              @unlink($plus['zip']);
              // 删除封面图
              @unlink($plus['thumb']);
              $res = ['status'=>200,'msg'=>"删除成功"];
              return $res;
         }
        $res = ['status'=>400,'msg'=>"删除失败"];
        return $res;
    }

    public function _uploadfile($File,$PATH){
        if(is_dir($PATH)){
            // 生成文件名
            $file_name = time().getRandNum(10);
            import("Class.pinyin",APP_PATH);
            // 中文转拼音
            $filename = pinyin::utf8_to(substr($File['name'],0,strrpos($File['name'],'.')));
            $fileFix = substr($File['name'],strrpos($File['name'],'.'));
            // 组合路径名
            $new_file_P = $PATH.$file_name.$filename.$fileFix;
            if($File['size']>C('UploadSize')){
                $res = array(
                    'code'=> 400,
                    'msg'=> "文件大小不符合要求"
                );
                return $res;
                exit();
            }

            // 上传文件
            if(is_uploaded_file($File['tmp_name'])){
                if(move_uploaded_file($File['tmp_name'],$new_file_P)){
                    $res = array(
                        'code'=> 200,
                        'msg'=> "上传成功"
                    );
                }else{
                    $res = array(
                        'code'=> 400,
                        'msg'=> "上传失败"
                    );
                }
            }

            $res['file'] = $File['name'];
            $res['type'] = $File['type'];
            $res['savepath'] = $new_file_P;
            return $res;
        }
    }
}